Long only 1/n portfolio#
import pandas as pd
pd.options.plotting.backend = "plotly"
import yfinance as yf
from cvx.simulator.builder import builder
from cvx.simulator.grid import resample_index
data = yf.download(tickers = "SPY AAPL GOOG MSFT", # list of tickers
period = "10y", # time period
interval = "1d", # trading interval
prepost = False, # download pre/post market hours data?
repair = True) # repair obvious price errors e.g. 100x?
[ 0%% ]
[**********************50%% ] 2 of 4 completed
[**********************75%%********** ] 3 of 4 completed
[*********************100%%**********************] 4 of 4 completed
prices = data["Adj Close"]
capital = 1e6
b = builder(prices=prices, initial_cash=capital)
for time, state in b:
# each day we invest a quarter of the capital in the assets
b[time[-1]] = 0.25 * state.nav / state.prices
portfolio = b.build()
portfolio.profit.cumsum().plot()
portfolio.nav.plot()
Rebalancing#
Usually we would not execute on a daily basis but rather rebalance every week, month or quarter. There are two approaches to deal with this problem in cvxsimulator.
Resample the existing daily portfolio (helpful to see effect of your hesitated trading)
Trade only on days that are within a predefined grid (most flexible if you have a rather irregular grid)
Resample an existing portfolio#
portfolio_resampled = portfolio.resample(rule="M")
frame = pd.DataFrame({"original": portfolio.nav, "monthly": portfolio_resampled.nav})
frame
| original | monthly | |
|---|---|---|
| Date | ||
| 2013-10-28 | 1.000000e+06 | 1.000000e+06 |
| 2013-10-29 | 9.999858e+05 | 9.999858e+05 |
| 2013-10-30 | 1.001458e+06 | 1.001323e+06 |
| 2013-10-31 | 9.988216e+05 | 9.987012e+05 |
| 2013-11-01 | 9.981311e+05 | 9.980086e+05 |
| ... | ... | ... |
| 2023-10-23 | 7.064971e+06 | 7.047425e+06 |
| 2023-10-24 | 7.117703e+06 | 7.100342e+06 |
| 2023-10-25 | 6.951941e+06 | 6.932309e+06 |
| 2023-10-26 | 6.778848e+06 | 6.757513e+06 |
| 2023-10-27 | 6.794047e+06 | 6.773228e+06 |
2518 rows × 2 columns
print(portfolio_resampled.stocks)
AAPL GOOG MSFT SPY
Date
2013-10-28 15136.658020 9889.145480 8392.969658 1702.018737
2013-10-29 15136.658020 9889.145480 8392.969658 1702.018737
2013-10-30 15136.658020 9889.145480 8392.969658 1702.018737
2013-10-31 15136.658020 9889.145480 8392.969658 1702.018737
2013-11-01 15394.545591 9754.949763 8386.715633 1699.030524
... ... ... ... ...
2023-10-23 10106.547951 12991.142489 5456.845162 4109.458511
2023-10-24 10106.547951 12991.142489 5456.845162 4109.458511
2023-10-25 10106.547951 12991.142489 5456.845162 4109.458511
2023-10-26 10106.547951 12991.142489 5456.845162 4109.458511
2023-10-27 10106.547951 12991.142489 5456.845162 4109.458511
[2518 rows x 4 columns]
# almost hard to see that difference between the original and resampled portfolio
frame.plot()
# number of shares traded
portfolio_resampled.trades_stocks.iloc[1:].plot()
Trade only days in predefined grid#
b = builder(prices=prices, initial_cash=capital)
# define a grid
grid = resample_index(prices.index, rule="M")
for time, state in b:
# each day we invest a quarter of the capital in the assets
if time[-1] in grid:
b[time[-1]] = 0.25 * state.nav / state.prices
else:
# forward fill an existing position
b[time[-1]] = b[time[-2]]
portfolio = b.build()
portfolio.nav.plot()
# Trading only once a month can lead to days where 150k had to be reallocated
portfolio.turnover.iloc[1:].plot()
Why not resampling the prices?#
I don’t believe in bringing the prices to a monthly grid. This would render it hard to construct signals given the sparse grid. We stay on a daily grid and trade once a month.